<?php

class Order_Service_StatisticsProccess
{
	protected $_userId = null;
	
	public function __construct(){			
		$this->setUserId();
	}
	
	private function setUserId(){
		$session = new Zend_Session_Namespace('userAuth');
		$this->_userId = $session->userID;		
	}
	
	/**
	 *  更新或创建 统计数据
	 * @param array $row
	 * $row['ot_order_cnt'] '订单个数'
	 * $row['ot_sku_cnt']	'订单行数'
	 * @param $cond 一维数组，$cond['user_id']、$cond['ot_date']、$cond['ot_class']，
	 * $cond['ot_date'] 格式：2012-06-05 06:07:08
	 * 其中$cond['ot_class']不能为空。
	 * @return unknown
	 */
	public function statisticsEdit(array $row,array $cond){
		if(empty($cond['user_id'])){
			$cond['user_id'] = $this->_userId;
		}
		if(empty($cond['ot_date'])){	//为空的话，使用当前时间
			$cond['ot_date'] = date('Y-m-d');
			if(0< date('Gis') && date('Gis')< 80000){
				$cond['ot_date'] = date('Y-m-d',strtotime('-1 day'));
			}
		}else{
			$timestamp = strtotime($cond['ot_date']);
			if(0< date('Gis',$timestamp) && date('Gis',$timestamp)< 80000){
				$cond['ot_date'] = date('Y-m-d',$timestamp-60*60*24);				
			}
		}
		
		if(empty($cond['ot_class'])){
			throw new Exception(' 统计类型不能为空.');
		}
		
		$res = Order_Service_OrderStatistics::getRowByCond($cond);
		$ctime = date('Y-m-d H:i:s');
		if(!empty($res)){
			$row['ot_last_update'] = $ctime;
			$row['ot_order_cnt'] = $res['ot_order_cnt'] + $row['ot_order_cnt'];
			$row['ot_sku_cnt'] = $res['ot_sku_cnt'] + $row['ot_sku_cnt'];
			$affect = Order_Service_OrderStatistics::updateByCond($row,$cond);
			return $affect;
		}else {
			$row['ot_create_time'] = $ctime;
			$row['ot_last_update'] = $ctime;
			$newRow = array_merge($row,$cond);
			$insert = Order_Service_OrderStatistics::add($newRow);
			return $insert;
		}
		
	}
	
}